import axios from 'axios'
import storage from './storage'
import { Message } from 'element-ui'
import qs from 'qs'

axios.defaults.timeout = 15000
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

// 添加请求拦截器，在请求头中加token
axios.interceptors.request.use(
  config => {
    let token = storage.get('token')
    if (token) {
      config.headers.common['Authorization'] = 'Bearer ' + token
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

axios.interceptors.response.use(
  response => { 
    return response;
  },
  error => {
    if(error.message.includes('timeout')){
      Message({
        message: '请求超时!',
        showClose: true,
        type: 'error'
      });
    }
    return Promise.reject(error)
  }
)

export default axios